import pygame
import api
import main
from _thread import start_new_thread as run
from api import screen

pygame.init()
player_x = player_y = 0


def ai(entity: api.Entity):
    clock = pygame.time.Clock()
    while True:
        a = entity.ai()
        if a is not None and (not a):
            break
        clock.tick(entity.ticks)


for entity in api.entitylist:
    if entity.type != 1:
        run(ai, (entity,))

while api.Run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            api.Run = False
            break
        elif event.type == pygame.KEYDOWN:
            for entity in api.entitylist:
                entity: api.Entity
                entity.go(event)
    screen.fill((0, 0, 0))
    api.showBG(screen)
    if api.showBlock:
        for block in api.blocklist:
            block: api.Block
            # print(block.__dict__)
            screen.blit(block.texture, block.pos)
    if api.showEntity:
        for entity in api.entitylist:
            screen.blit(entity.texture, entity.pos)
    api.showUI(screen)
    if api.runFunc:
        for f in api.functionList:
            f()
    pygame.display.update()

pygame.quit()
print("游戏结束")
